// pages/mine/mine.js
const App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userPhoto: '',
    userName: '',
    wxPhoto: true,
    wxNickname: true,
    isVip: false, //是否vip
    followNum:0, //关注人数
    fansNum:0, //粉丝人数
    recommandNum:0, //种草推荐
    vipDesc:'首月5元，日常15元/月',// vip描述
    yuMoney:'0', //余额
    youhui:'0', //优惠券
    jifen:'0', //积分
    redbag:'0', //红包
  },

  /**
   * 生命周期函数--监听页面加载 
   */
  onLoad: function (options) {
    this.getCouponNum();//获取优惠券数量
    this.getMoneyList();//获取红包记录
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //判断是不是vip
    let msg = wx.getStorageSync('allUserMsg');
    if (msg.vip.is_vip) {
      this.setData({
        isVip: true,
        vipDesc: msg.vip.expire + '到期'
      })
    } else {
      this.setData({
        isVip: false,
      })
      this.getVipInfo();//获取vip信息
    } 
    
    let info = wx.getStorageSync('userInfo')
    let defaultName = info.openid.slice(0, 10);

    if (info.nickname == defaultName || info.nickname == null || info.nickname == 'null' || info.nickname == info.phone) {
      this.setData({
        wxNickname: true
      })
    } else {
      this.setData({
        wxNickname: false,
        userName: info.nickname,
      })
    }

    if (info.userlogo == '' || info.userlogo == null) {
      this.setData({
        wxPhoto: true,
      })
    } else {
      this.setData({
        wxPhoto: false,
        userPhoto: info.userlogo
      });
    }

    this.getMoneyTotal();//钱包余额  
    this.gettitleNumMsg();//获取关注粉丝数量
    this.getJifenInfo();// 获取积分信息
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //钱包金额
  getMoneyTotal: function () {
    let that = this;
    wx.request({
      url: App.data.globalAPI + '/users/wallet/info',
      data: {
        token: wx.getStorageSync('token')
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.error == 0) {
          that.setData({
            yuMoney: res.data.data.money
          })
        } else if (res.data.error == 100009 || res.data.error == 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
          setTimeout(function () {
            wx.reLaunch({
              url: '../index/index',
            })
          }, 500)
        }
      },
      fail(res) {
        wx.showModal({
          title: '提示',
          content: '网络异常，请稍后重试！',
          showCancel: false,
          success(res) {
            wx.reLaunch({
              url: '../index/idnex',
            })
          }
        })
      }
    })
  },
  //获取优惠券数量
  getCouponNum: function () {
    let that = this;
    wx.request({
      url: App.data.globalAPI + '/coupon/user/getlist',
      data: {
        type: that.data.currentTab,
        start: 0,
        num: 1000,
        userId: wx.getStorageSync('userInfo').uid,
        token: wx.getStorageSync('token')
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.error == 0) {
          that.setData({
            youhui: res.data.data.items.length
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },

  //vip信息
  getVipInfo: function(){
    let that = this;
    wx.request({
      url: App.data.globalAPI + '/users/vip/level',
      data: {
        token: wx.getStorageSync('token')
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.error == 0) {
          that.setData({
            vipDesc: res.data.data[0].name +': '+ res.data.data[0].price + '/月',
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 800
          })
        }
      }
    })
  },

  // 商城服务订单
  goMallService:function(){
    let pageUrl = App.data.globalMall + '/#/order_list?dev=1&token='+wx.getStorageSync('token');
    wx.navigateTo({
      url: '/pages/apply/web_page?url=' + encodeURIComponent(pageUrl)
    })
  },

  // 获取关注粉丝数量
  gettitleNumMsg:function(){
    let that = this;
    wx.request({
      url: App.data.myFansNum +'/ugc/personal/getnumber',
      data: {
        token: wx.getStorageSync('token')
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.error == 0) {
          that.setData({
            followNum: res.data.data.followNumber, //关注人数
            fansNum: res.data.data.fansNumber, //粉丝人数
            recommandNum: res.data.data.articleNumber //种草推荐
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 800
          })
        }
      }
    })
  },

  // 去关注页面
  goGuanzhuPage: function(){
    let pageUrl = App.data.globalUGC + '/myConcern?token=' + wx.getStorageSync('token')
    wx.navigateTo({
      url: '/pages/apply/web_page?url='+encodeURIComponent(pageUrl)
    })
  },

  // 去粉丝页面
  goFansPage:function(){
    let pageUrl = App.data.globalUGC + '/myFens?token=' + wx.getStorageSync('token')
    wx.navigateTo({
      url: '/pages/apply/web_page?url=' + encodeURIComponent(pageUrl)
    })
  },

  // 我的主页
  toUserInformation:function(){
    let pageUrl = App.data.globalUGC + '/user?token=' + wx.getStorageSync('token')
    wx.navigateTo({
      url: '/pages/apply/web_page?url=' + encodeURIComponent(pageUrl)
    })
  },

  // 种草页面
  zhongcaoPage:function(){
    let pageUrl = App.data.globalUGC + '/zhongcaoyutuijian?token=' + wx.getStorageSync('token');
    wx.navigateTo({
      url: '/pages/apply/web_page?url=' + encodeURIComponent(pageUrl)
    })
  },

  //获取红包记录列表
  getMoneyList: function () {
    let that = this;
    wx.request({
      url: App.data.globalAPI + '/users/redpacket/getlist',
      data: {
        token: wx.getStorageSync('token'),
        start: 0,
        num: 1000
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.error == 0) {
          let list = res.data.data.items;
          that.setData({
            redbag: list.length
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 600
          })
        }
      }
    })
  },

  //积分信息
  getJifenInfo: function () {
    let that = this;
    wx.request({
      url: App.data.globalAPI + '/users/integral/info',
      data: {
        token: wx.getStorageSync('token'),
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.error == 0) {
          that.setData({
            jifen: res.data.data.usable
          })
          wx.hideLoading();
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 600
          })
        }
      }
    })
  }

})